def av(x):
    f = 0
    for i in range(2,x):
        if x % i == 0:
            f= f + 1
    if f != 0:
        return False
    return True
def mfsdr(x,a):
    if str(x) in str(a):
        return True
    return False

a = int(input())
o = 0
if 999 < a <= 9999:
    for i in range(a+1):
        if av(i) and mfsdr(i,a):
            if o < i:
                o = i
print(o)
